package com.fengye.fdfs;

import org.csource.fastdfs.*;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @author fengyexjtu@126.com
 * @date 2022年05月22日 6:29 PM
 */
@Component
public class FastdfsClientConfig {
    private static final String CONFIG_FILENAME = "fastdfs-client.properties";

    @Bean
    public StorageClient1 storageClient1() {
        try {
            ClientGlobal.initByProperties(CONFIG_FILENAME);
        } catch (Exception e) {
            throw new RuntimeException("fastdfs 初始化错误");
        }
        System.out.println("ClientGlobal.configInfo(): " + ClientGlobal.configInfo());
        TrackerClient trackerClient = new TrackerClient(ClientGlobal.g_tracker_group);
        TrackerServer trackerServer = null;
        try {
            trackerServer = trackerClient.getTrackerServer();
        } catch (IOException e) {
            throw new RuntimeException("trackerServer 找不到");
        }
        if (trackerServer == null) {
            throw new IllegalStateException("getConnection return null");
        }

        StorageServer storageServer = null;
        try {
            storageServer = trackerClient.getStoreStorage(trackerServer);
        } catch (Exception e) {
            throw new IllegalStateException("getStoreStorage return null");
        }

        if (storageServer == null) {
            throw new IllegalStateException("storageServer 为空");
        }

        return new StorageClient1(trackerServer, storageServer);
    }
}
